package com.ksd.web.controller.service;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.StringUtils;
import com.ksd.framework.web.domain.server.Sys;
import com.ksd.quartz.http.DRMHttpClient;
import com.ksd.quartz.task.beans.*;
import com.ksd.quartz.util.TaskConstans;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import com.ksd.web.controller.m3beans.*;
import com.ksd.web.controller.networkBean.*;
import com.ksd.web.controller.tool.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.ksd.common.core.domain.AjaxResult.error;

@Service
public class SendChannelService {

    public final Logger logger = LoggerFactory.getLogger(SendChannelService.class);
    @Autowired
    private ISysOperLogService sysOperLogService;
    @Autowired
    private ISysBoardService sysBoardService;
    @Autowired
    private ISysNetworkService sysNetworkService;
    @Autowired
    private ISysChannelService sysChannelService;
    @Autowired
    private ISysFreqService sysFreqService;

    public String getServiceInfoService(String url) {

        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }


        GetChannelM3Request m3RequestInfo = GetChannelM3Request.getM3RequestInfo(TaskConstans.getServiceInfo);
        ChannelParameters channelParameters = new ChannelParameters();
        channelParameters.setSymbolRate(6875);
//        channelParameters.setPolarization(2);
        channelParameters.setFrequency(0);
//        channelParameters.setDeliveryType(10);
        channelParameters.setModulation(3);
        channelParameters.setBouquetID(24912);
        m3RequestInfo.setParameters(channelParameters);
        Map<String, String> extHeader = new HashMap<String, String>();

        String requestGson = JSONObject.toJSONString(m3RequestInfo);

        logger.info("[system-m3] getServiceInfo Interface url :" + url);
        logger.info("[system-m3] getServiceInfo Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        return response;
    }

    public String getPrfData(String url) {

        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }

        GetChannelM3Request m3RequestInfo = GetChannelM3Request.getM3RequestInfo(TaskConstans.getPrfDataMethod);
        ChannelParameters channelParameters = new ChannelParameters();
        m3RequestInfo.setParameters(channelParameters);
        Map<String, String> extHeader = new HashMap<String, String>();

        String requestGson = JSONObject.toJSONString(m3RequestInfo);

        logger.info("[system-m3] getPrfData Interface url :" + url);
        logger.info("[system-m3] getPrfData Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        logger.info("[system-m3] getPrfData Interface Response :" + response);
        return response;
    }

    public String setPrfData(List<SaveServiceInfo> saveServiceInfos,String url,String board) {
        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }

        SavePrfDataRequest savePrfDataRequest = SavePrfDataRequest.getSavePrfDataRequest(TaskConstans.setPrfDataMethod);
        savePrfDataRequest = setPrfDataByServiceArrayInfo(saveServiceInfos, board, savePrfDataRequest);
        Map<String, String> extHeader = new HashMap<String, String>();

        String requestGson = JSONObject.toJSONString(savePrfDataRequest);

        logger.info("[system-m3] setPrfData Interface url :" + url);
        logger.info("[system-m3] setPrfData Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        logger.info("[system-m3] setPrfData Interface Response :" + response);
        return response;
    }

    public SavePrfDataRequest setPrfDataByServiceArrayInfo(List<SaveServiceInfo> saveServiceInfos,String board,SavePrfDataRequest savePrfDataRequest) {

//        SaveServiceInfo saveServiceInfo1= saveServiceInfos.get(0);   //前端页面只会向后台发送当前高频头选中的频道
//        SaveServiceInfo saveServiceInfo2 = saveServiceInfos.get(1);

//        List<SaveService> serviceinfo1 = saveServiceInfo1.getServiceinfo();
//        List<SaveService> serviceinfo2 = saveServiceInfo2.getServiceinfo();
        savePrfDataRequest.setParameters(saveServiceInfos);

        return  savePrfDataRequest;
    }

    public String setMainFreqEx(String url,MainFreqParameters parameters) {
        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }
        MainFreqRequest m3RequestInfo = MainFreqRequest.setMainFreqRequest(TaskConstans.setMainFreqScan);
        m3RequestInfo.setParameters(parameters);
        String requestGson = JSONObject.toJSONString(m3RequestInfo);

        logger.info("[system-m3] setMainFreqEx Interface url :" + url);
        logger.info("[system-m3] setMainFreqEx Interface Request " + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        logger.info("[system-m3] startChannelScan Interface Response :" + response);
        return response;
    }



    public String startChannelScan(StartChannelParameter startChannelParameter,String url) {


        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }


        StartChannelScanRequest m3RequestInfo = StartChannelScanRequest.startChannelScanRequest(TaskConstans.startChannelScan);
        m3RequestInfo.setParameters(startChannelParameter);
        String requestGson = JSONObject.toJSONString(m3RequestInfo);

        logger.info("[system-m3] startChannelScan Interface url :" + url);
        logger.info("[system-m3] startChannelScan Interface Request " + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        logger.info("[system-m3] startChannelScan Interface Response :" + response);
        return response;
    }

    public String networkLoginService(String url) throws Exception {
        M3LoginRequest m3RequestInfo = M3LoginRequest.getM3RequestInfo(TaskConstans.loginMethod);
        String requestStr = JSONUtil.objectToStr(m3RequestInfo);

        logger.info("[system-login-m3] user login url :" + url);
        logger.info("[system-login-m3] user login request :" + requestStr);
        String response = DRMHttpClient.udrmHttpDoPostOutTime(url, requestStr);
        logger.info("[system-login-m3] user login response :" + response);

        if (StringUtils.isEmpty(response)) {
            logger.error("账号登录失败");
            sysOperLogService.insertOperlogApiError("用户登录失败.请求地址: "+url+",接口响应数据为空，请检查网络连接或联系管理员稍后重试");
            return null;
        } else {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                // 将 JSON 字符串解析为 JsonNode 对象
                JsonNode rootNode = objectMapper.readTree(response);
                // 获取 status 节点的值
                int status = rootNode.get("status").asInt();
                if (status != 0){
                    logger.error("账号登录失败，请联系管理员检查登录接口服务");
                    sysOperLogService.insertOperlogApiError("用户登录接口.请求地址: "+url+",响应状态显示登录失败，请检查登录接口服务或联系管理员稍后重试");
                    return null;
                }else {
                    return response;
                }
            } catch (JsonProcessingException e) {
                logger.error(e.getMessage());
                return null;
            }
        }
    }


    public String loginService(String url) {
        M3LoginRequest m3RequestInfo = M3LoginRequest.getM3RequestInfo(TaskConstans.loginMethod);
        String requestStr = JSONUtil.objectToStr(m3RequestInfo);
//        String url = "http://192.168.100.249/m3";

        logger.info("[system-login-m3] user login url :" + url);
        logger.info("[system-login-m3] user login request :" + requestStr);
        String response = DRMHttpClient.udrmHttpDoPost(url, requestStr);
        logger.info("[system-login-m3] user login response :" + response);

        if (StringUtils.isEmpty(response)) {
            logger.error("账号登录失败");
            sysOperLogService.insertOperlogApiError("用户登录失败.请求地址: "+url+",接口响应数据为空，请检查网络连接或联系管理员稍后重试");
            return null;
        } else {


            try {
                ObjectMapper objectMapper = new ObjectMapper();
                // 将 JSON 字符串解析为 JsonNode 对象
                JsonNode rootNode = objectMapper.readTree(response);
                // 获取 status 节点的值
                int status = rootNode.get("status").asInt();
                if (status != 0){
                    logger.error("账号登录失败，请联系管理员检查登录接口服务");
                    sysOperLogService.insertOperlogApiError("用户登录接口.请求地址: "+url+",响应状态显示登录失败，请检查登录接口服务或联系管理员稍后重试");
                    return null;
                }else {
                    return response;
                }
            } catch (JsonProcessingException e) {
                logger.error(e.getMessage());
                return null;
            }
        }
    }

    public String stopChannelScan(String url) {

        StopChannelScanRequest m3RequestInfo = StopChannelScanRequest.stopChannelScanRequest(TaskConstans.stopChannelScan);
        String requestGson = JSONObject.toJSONString(m3RequestInfo);

        logger.info("[system-m3] stopChannelScan Interface url :" + url);
        logger.info("[system-m3] stopChannelScan Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        logger.info("[system-m3] stopChannelScan Interface Response :" + response);
        return response;
    }



    public String resetProgramService(String url) {

        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }
        StopChannelScanRequest m3RequestInfo = StopChannelScanRequest.stopChannelScanRequest(TaskConstans.resetProgramDbs);
        String requestGson = JSONObject.toJSONString(m3RequestInfo);

        logger.info("[system-m3] resetProgramDbs Interface url :" + url);
        logger.info("[system-m3] resetProgramDbs Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        logger.info("[system-m3] resetProgramDbs Interface Response :" + response);
        return response;
    }

    public String resetSetPrfDataService(String url) {

        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }

        SavePrfDataRequest m3RequestInfo = SavePrfDataRequest.getSavePrfDataRequest(TaskConstans.setPrfDataMethod);
        List<SaveServiceInfo> list = new ArrayList<>();
        m3RequestInfo.setParameters(list);
        String requestGson = JSONObject.toJSONString(m3RequestInfo);

        logger.info("[system-m3] resetSetPrfDataService Interface url :" + url);
        logger.info("[system-m3] resetSetPrfDataService Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(url, requestGson);
        logger.info("[system-m3] resetSetPrfDataService Interface Response :" + response);
        return response;
    }

    public AjaxResult getChannelListManage(Long boardId){
        SysBoard sysBoard = sysBoardService.selectSysBoardById(boardId);
        String serviceUrl = sysBoard.getUrl();
        String response = getServiceInfoService(serviceUrl);
        if (StringUtils.isEmpty(response)) {
            sysOperLogService.insertOperlogApiError("频道获取接口.请求地址: "+serviceUrl+",响应超时,请检查网络连接或联系管理员稍后重试");
            return error("频道获取接口响应超时,请检查网络连接或联系管理员稍后重试");
        } else {
            ObjectMapper mapperPage = new ObjectMapper();
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                // 将 JSON 字符串解析为 JsonNode 对象
                JsonNode rootNode = objectMapper.readTree(response);
                // 获取 status 节点的值
                int statusInt = rootNode.get("status").asInt();
                if (statusInt == 0) {
                    // 当 status 为 0 时，获取 data 节点
                    // 进一步处理 data 节点，这里可以根据 data 对象的实际结构进行解析
                    try {
                        SynchChannelResponse synchChannelResponse = mapperPage.readValue(response, SynchChannelResponse.class);
                        logger.info("[system-channelManage] The check channel manage Interface return data status is " + statusInt);
                        ResponseData data = synchChannelResponse.getData();
                        if (StringUtils.isNotNull(data)) {
                            logger.info("开始进行频道数据表的更新");
                            List<ChannelInfo> info = data.getInfo();
                            logger.info("查询到的频道信息:"+info.size()+"============直播板卡:"+boardId);
                            if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                sysChannelService.clearSysChannel(SysChannel.table_one);
                                sysFreqService.clearSysFreq(SysFreq.table_one);
                            }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                sysChannelService.clearSysChannel(SysChannel.table_twice);
                                sysFreqService.clearSysFreq(SysFreq.table_twice);
                            }
                            for (ChannelInfo channelInfo : info) {
                                Integer type = channelInfo.getType();
                                if (type == 1){
                                    logger.info("获取直播板卡:"+boardId+"==============的频道信息：");
//                                    logger.info(channelInfo.toString());
                                    //电视频道
                                    SysChannel sysChannel = new SysChannel();
                                    if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                        sysChannel = sysChannelService.selectSysChannelByName(channelInfo.getName(),SysChannel.table_one);
                                    }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                        sysChannel = sysChannelService.selectSysChannelByName(channelInfo.getName(),SysChannel.table_twice);
                                    }
                                    logger.info("进入数据库查询"+"=============="+channelInfo.getName());
                                    if (StringUtils.isNotNull(sysChannel)) {
                                        logger.info("数据库中能够查询到:更新:"+sysChannel.getName());
                                        if (StringUtils.isNotNull(channelInfo.getType())) {
                                            sysChannel.setType(Long.valueOf(channelInfo.getType()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getName())) {
                                            sysChannel.setName(channelInfo.getName());
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getFreq())) {
                                            sysChannel.setFreq(Long.valueOf(channelInfo.getFreq()));
                                        }
                                        Integer symbolRate = channelInfo.getSymbolRate();
                                        if (symbolRate != null) {
                                            sysChannel.setSymbolRate(Long.valueOf(channelInfo.getSymbolRate()));
                                        }
                                        Integer polarization = channelInfo.getPolarization();
                                        if (polarization != null) {
                                            sysChannel.setPolarization(Long.valueOf(channelInfo.getPolarization()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getPmtpid())) {
                                            sysChannel.setPmtpid(Long.valueOf(channelInfo.getPmtpid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getOnid())) {
                                            sysChannel.setOnid(Long.valueOf(channelInfo.getOnid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getTsid())) {
                                            sysChannel.setTsid(Long.valueOf(channelInfo.getTsid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getSvid())) {
                                            sysChannel.setSvid(Long.valueOf(channelInfo.getPmtpid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getPmtpid())) {
                                            sysChannel.setPmtpid(Long.valueOf(channelInfo.getPmtpid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getAudiopid())) {
                                            Integer[] audiopid = channelInfo.getAudiopid();
                                            sysChannel.setAudiopid(getStringByIntArr(audiopid));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getAudiotype())) {
                                            Integer[] audiotype = channelInfo.getAudiotype();
                                            sysChannel.setAudiotype(getStringByIntArr(audiotype));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getVideopid())) {
                                            Integer[] videopid = channelInfo.getVideopid();

                                            sysChannel.setVideopid(getStringByIntArr(videopid));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getVideotype())){
                                            Integer[] videotype = channelInfo.getVideotype();

                                            sysChannel.setVideotype(getStringByIntArr(videotype));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getGroupids())) {
                                            Integer[] groupids = channelInfo.getGroupids();

                                            sysChannel.setGroupids(getStringByIntArr(groupids));
                                        }

                                        if (StringUtils.isNotNull(channelInfo.getDeliveryType())) {
                                            sysChannel.setDeliveryType(Long.valueOf(channelInfo.getDeliveryType()));
                                        }
                                        sysChannel.setBoardId(boardId);

                                        if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                            sysChannelService.updateSysChannel(sysChannel,SysChannel.table_one);
                                        }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                            sysChannelService.updateSysChannel(sysChannel,SysChannel.table_twice);
                                        }
                                    } else {
                                        logger.info("===========================数据库中没有查询到:新增:"+channelInfo.getName());
                                        sysChannel = new SysChannel();
                                        if (StringUtils.isNotNull(channelInfo.getType())) {
                                            sysChannel.setType(Long.valueOf(channelInfo.getType()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getName())) {
                                            sysChannel.setName(channelInfo.getName());
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getFreq())) {
                                            sysChannel.setFreq(Long.valueOf(channelInfo.getFreq()));
                                        }
                                        Integer symbolRate = channelInfo.getSymbolRate();
                                        if (symbolRate != null) {
                                            sysChannel.setSymbolRate(Long.valueOf(channelInfo.getSymbolRate()));
                                        }
                                        Integer polarization = channelInfo.getPolarization();
                                        if (polarization != null) {
                                            sysChannel.setPolarization(Long.valueOf(channelInfo.getPolarization()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getPmtpid())) {
                                            sysChannel.setPmtpid(Long.valueOf(channelInfo.getPmtpid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getOnid())) {
                                            sysChannel.setOnid(Long.valueOf(channelInfo.getOnid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getTsid())) {
                                            sysChannel.setTsid(Long.valueOf(channelInfo.getTsid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getSvid())) {
                                            sysChannel.setSvid(Long.valueOf(channelInfo.getPmtpid()));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getPmtpid())) {
                                            sysChannel.setPmtpid(Long.valueOf(channelInfo.getPmtpid()));
                                        }

                                        if (StringUtils.isNotNull(channelInfo.getAudiotype())) {
                                            Integer[] audiotype = channelInfo.getAudiotype();
                                            sysChannel.setAudiotype(getStringByIntArr(audiotype));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getVideopid())) {
                                            Integer[] videopid = channelInfo.getVideopid();
                                            sysChannel.setVideopid(getStringByIntArr(videopid));

                                        }
                                        if (StringUtils.isNotNull(channelInfo.getVideotype())){
                                            Integer[] videotype = channelInfo.getVideotype();
                                            sysChannel.setVideotype(getStringByIntArr(videotype));
                                        }
                                        if (StringUtils.isNotNull(channelInfo.getGroupids())) {
                                            Integer[] groupids = channelInfo.getGroupids();
                                            sysChannel.setGroupids(getStringByIntArr(groupids));
                                        }

                                        if (StringUtils.isNotNull(channelInfo.getDeliveryType())) {
                                            sysChannel.setDeliveryType(Long.valueOf(channelInfo.getDeliveryType()));
                                        }


                                        sysChannel.setBoardId(boardId);
                                        sysChannel.setStatus1(TaskConstans.status_init);
                                        sysChannel.setStatus2(TaskConstans.status_init);
                                        if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                            sysChannelService.insertSysChannel(sysChannel,SysChannel.table_one);
                                        }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                            logger.info("板卡2将最新获取到的频道新增:"+sysChannel.getName());
                                            sysChannelService.insertSysChannel(sysChannel,SysChannel.table_twice);
                                        }
                                    }
//                                    logger.info("频道更新成功:"+channelInfo.getName());
                                }
                            }

//                            logger.info("频道频率表进行更新:"+info.size());
                            for (ChannelInfo channelInfo : info) {
                                Integer type = channelInfo.getType();
                                if(type == 1){
                                    Integer freq = channelInfo.getFreq();
                                    Integer polarization = channelInfo.getPolarization();
                                    Integer symbolRate = channelInfo.getSymbolRate();
                                    SysFreq sysFreq = new SysFreq();
                                    if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                        sysFreq = sysFreqService.selectSysFreqByFreq(Long.valueOf(freq),SysFreq.table_one);
                                    }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                        sysFreq = sysFreqService.selectSysFreqByFreq(Long.valueOf(freq),SysFreq.table_twice);
                                    }
                                    if (StringUtils.isNull(sysFreq)) {
                                        sysFreq = new SysFreq();
                                        sysFreq.setFreq(Long.valueOf(freq));
                                        if (polarization != null) {
                                            sysFreq.setPolarization(Long.valueOf(polarization));
                                        }
                                        if (symbolRate != null) {
                                            sysFreq.setSymbol(Long.valueOf(symbolRate));
                                        }
                                        sysFreq.setBoardId(Long.valueOf(boardId));

                                        if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                            sysFreqService.insertSysFreq(sysFreq,SysFreq.table_one);
                                        }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                            sysFreqService.insertSysFreq(sysFreq,SysFreq.table_twice);
                                        }
                                    }else {
                                        sysFreq.setFreq(Long.valueOf(freq));
                                        if (polarization != null) {
                                            sysFreq.setPolarization(Long.valueOf(polarization));
                                        }
                                        if (symbolRate != null) {
                                            sysFreq.setSymbol(Long.valueOf(symbolRate));
                                        }
                                        sysFreq.setBoardId(Long.valueOf(boardId));
                                        if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                            sysFreqService.updateSysFreq(sysFreq,SysFreq.table_one);
                                        }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                            sysFreqService.updateSysFreq(sysFreq,SysFreq.table_twice);
                                        }
                                    }
                                }
                            }
//                            logger.info("对频率表进行排序");
                            //对频点进行排列序号
                            SysFreq numberFreq = new SysFreq();
                            numberFreq.setBoardId(boardId);
                            List<SysFreq> sysFreqs = new ArrayList<>();
                            if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                sysFreqs =  sysFreqService.selectSysFreqListByFreq(numberFreq,SysFreq.table_one);
                                for (int i = 0; i < sysFreqs.size(); i++) {
                                    SysFreq sysFreq = sysFreqs.get(i);
                                    sysFreq.setNumber(Long.valueOf(i+1));
                                    sysFreqService.updateSysFreq(sysFreq,SysFreq.table_one);
                                }
                            }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                sysFreqs = sysFreqService.selectSysFreqListByFreq(numberFreq,SysFreq.table_twice);
                                for (int i = 0; i < sysFreqs.size(); i++) {
                                    SysFreq sysFreq = sysFreqs.get(i);
                                    sysFreq.setNumber(Long.valueOf(i+1));
                                    sysFreqService.updateSysFreq(sysFreq,SysFreq.table_twice);
                                }
                            }

//                            logger.info("更新频道表的ip信息");
                            SysChannel sysChannel = new SysChannel();
                            sysChannel.setBoardId(Long.valueOf(sysBoard.getId()));
                            List<SysChannel> sysChannelsList = new ArrayList<>();
                            String tempIp = "";
                            if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                sysChannelsList = sysChannelService.selectSysChannelList(sysChannel, SysChannel.table_one);
                                tempIp = PlayControlConfig.getChannelOneIpStart();
                            }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                sysChannelsList = sysChannelService.selectSysChannelList(sysChannel, SysChannel.table_twice);
                                tempIp = PlayControlConfig.getChannelTwiceIpStart();
                            }
                            //重新对获取到的频道ip进行编排
                            Integer index = 0;
                            for (SysChannel channel : sysChannelsList) {
                                index++;
                                String ip = channel.getIp();
                                String port = channel.getPort();
                                if (StringUtils.isEmpty(ip)){
                                    channel.setIp(tempIp+index);
                                }
                                if (StringUtils.isEmpty(port)){
                                    channel.setPort(PlayControlConfig.getChannelPort());
                                }
                                if (TaskConstans.boardValue1.equals(String.valueOf(boardId))) {
                                    sysChannelService.updateSysChannel(channel, SysChannel.table_one);
                                }else if (TaskConstans.boardValue2.equals(String.valueOf(boardId))) {
                                    sysChannelService.updateSysChannel(channel, SysChannel.table_twice);
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        return error(e.getMessage());
                    }
                }else {
                    sysOperLogService.insertOperlogApiError("频道管理接口.请求地址: "+serviceUrl+",响应状态失败，请联系管理员检查接口服务或稍后重试");
                    return error("频道管理接口响应状态失败，请联系管理员检查接口服务或稍后重试");
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
                return error(e.getMessage());
            }
        }
        return AjaxResult.success("频道管理接口响应成功");
    }

    public String getStringByIntArr(Integer[] args) {
        // 模拟获取 audiotype 数组
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(args[i]);
        }
        return sb.toString();
    }

    public SysNetwork getIpMode1(String url, SysNetwork sysNetwork) throws Exception {
//        String loginReturn = loginService(url);
        String loginReturn = networkLoginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return null;
        }
        int index = url.lastIndexOf('/');
        String newUrl = "";
        if (index != -1){
            String substringStart = url.substring(0, index);
            newUrl = substringStart + TaskConstans.rgextIpUrl;
        }
        GetIpModeRequest getIpMode = GetIpModeRequest.getGetIpMode();
        String requestGson = JSONObject.toJSONString(getIpMode);

        logger.info("[system-m3] getIpMode1 Interface url :" + newUrl);
        logger.info("[system-m3] getIpMode1 Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(newUrl, requestGson);
//        logger.info("[system-m3] getIpMode1 Interface Response :" + response);
        sysOperLogService.insertOperlogApiSuccess("网络配置获取接口getIpMode1.请求地址: "+url+",响应数据:"+response);
        if (StringUtils.isEmpty(response)){
            sysOperLogService.insertOperlogApiError("网络配置获取接口getIpMode1.请求地址: "+newUrl+",响应超时,请联系管理员检查接口服务或稍后重试");
            return null;
        }
        ObjectMapper mapperPage = new ObjectMapper();
        GetIpModeResponse getIpModeResponse = null;
        try {
            getIpModeResponse = mapperPage.readValue(response, GetIpModeResponse.class);
            GetIpModeData data = getIpModeResponse.getData();
            List<IpObject> ip1 = data.getIp();
            IpObject ipObject = ip1.get(0);
            String ipaddr = ipObject.getIpaddr();
            Integer netlen = ipObject.getNetlen();

            sysNetwork.setIp(ipaddr);
            sysNetwork.setSubnetMask(netlen.toString());
            sysNetworkService.updateSysNetwork(sysNetwork);
            return sysNetwork;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean setNetworkConfig(String url, BatchCmdIp ip1, String gw) {

        String loginReturn = loginService(url);
        if (StringUtils.isEmpty(loginReturn)) {
            logger.error("登录接口失败");
            return false;
        }

        int index = url.lastIndexOf('/');
        String newUrl = "";
        if (index != -1){
            String substringStart = url.substring(0, index);
            newUrl = substringStart + TaskConstans.rgextSysUrl;
        }

        BatchCmdRequest request = BatchCmdRequest.setCmdParameters(ip1,gw);

        String requestGson = JSONObject.toJSONString(request);

        logger.info("[system-m3] setNetworkConfig Interface url :" + newUrl);
        logger.info("[system-m3] setNetworkConfig Interface Request :" + requestGson);

        DRMHttpClient.udrmHttpDoPostNoResponse(newUrl, requestGson);
        return true;
    }

    public SysNetwork getMethod2(String url,SysNetwork sysNetwork) {

        int index = url.lastIndexOf('/');
        String newUrl = "";
        if (index != -1){
            String substringStart = url.substring(0, index);
            newUrl = substringStart + TaskConstans.rgextIpUrl;
        }

        GetIpModeRequest getIpMode = GetIpModeRequest.getMethodGet();
        String requestGson = JSONObject.toJSONString(getIpMode);

        logger.info("[system-m3] get2 Interface url :" + newUrl);
        logger.info("[system-m3] get2 Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(newUrl, requestGson);
//        logger.info("[system-m3] get2 Interface Response :" + response);


        sysOperLogService.insertOperlogApiSuccess("网络配置获取接口get2.请求地址: "+url+",响应数据:"+response);
        if (StringUtils.isEmpty(response)){
            sysOperLogService.insertOperlogApiError("网络配置获取接口get2.请求地址: "+newUrl+",响应超时,请检查网络连接或联系管理员稍后重试");
            return null;
        }
        ObjectMapper mapperPage = new ObjectMapper();
        GetIpModeResponse2 getIpModeResponse = null;
        try {
            getIpModeResponse = mapperPage.readValue(response, GetIpModeResponse2.class);
            List<IpObject> data = getIpModeResponse.getData();
            IpObject ipObject = data.get(0);
            String ipaddr = ipObject.getIpaddr();
            Integer netlen = ipObject.getNetlen();

            sysNetwork.setIp(ipaddr);
            sysNetwork.setSubnetMask(netlen.toString());
            return sysNetwork;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public SysNetwork getMethod3(String url,SysNetwork sysNetwork) {

//        String loginReturn = loginService(url);
//        if (StringUtils.isEmpty(loginReturn)) {
//            logger.error("登录接口失败");
//            return null;
//        }

        int index = url.lastIndexOf('/');
        String newUrl = "";
        if (index != -1){
            String substringStart = url.substring(0, index);
            newUrl = substringStart + TaskConstans.rgextNetUrl;
        }

        MacRequest macRequest = MacRequest.getMacRequest();
        String requestGson = JSONObject.toJSONString(macRequest);

        logger.info("[system-m3] get3 Interface url :" + newUrl);
        logger.info("[system-m3] get3 Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(newUrl, requestGson);
//        logger.info("[system-m3] get3 Interface Response :" + response);


        sysOperLogService.insertOperlogApiSuccess("网络配置获取接口get3.请求地址: "+url+",响应数据:"+response);
        if (StringUtils.isEmpty(response)){
            sysOperLogService.insertOperlogApiError("网络配置获取接口get3.请求地址: "+newUrl+",响应超时,请检查网络连接或联系管理员稍后重试");
            return null;
        }
        ObjectMapper mapperPage = new ObjectMapper();
        MacResponse macResponse = null;
        try {
            macResponse = mapperPage.readValue(response, MacResponse.class);
            MacObject data = macResponse.getData();
            String mac = data.getMac();
            sysNetwork.setMac(mac);
            return sysNetwork;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    public SysNetwork rgextRoute(String url,SysNetwork sysNetwork) {

//        String loginReturn = loginService(url);
//        if (StringUtils.isEmpty(loginReturn)) {
//            logger.error("登录接口失败");
//            return null;
//        }

        int index = url.lastIndexOf('/');
        String newUrl = "";
        if (index != -1){
            String substringStart = url.substring(0, index);
            newUrl = substringStart + TaskConstans.rgextRouteUrl;
        }

        MacRequest macRequest = MacRequest.getRouteRequest();
        String requestGson = JSONObject.toJSONString(macRequest);

        logger.info("[system-m3] rgextRoute4 Interface url :" + newUrl);
        logger.info("[system-m3] rgextRoute4 Interface Request :" + requestGson);

        String response = DRMHttpClient.udrmHttpDoPost(newUrl, requestGson);
//        logger.info("[system-m3] rgextRoute4 Interface Response :" + response);


        sysOperLogService.insertOperlogApiSuccess("网络配置获取接口rgextRoute4.请求地址: "+url+",响应数据:"+response);
        if (StringUtils.isEmpty(response)){
            sysOperLogService.insertOperlogApiError("网络配置获取接口rgextRoute4.请求地址: "+newUrl+",响应超时,请检查网络连接或联系管理员稍后重试");
            return null;
        }
        ObjectMapper mapperPage = new ObjectMapper();
        GetRouteResponse getRouteResponse = null;
        try {
            getRouteResponse = mapperPage.readValue(response, GetRouteResponse.class);
            GetRouteData getRouteData = getRouteResponse.getData().get(0);
            String gw = getRouteData.getGw();
            sysNetwork.setGateway(gw);
            return sysNetwork;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
